package com.joker.microstoremanager.controller;

import com.joker.microstoremanager.model.*;
import com.joker.microstoremanager.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping()
@Slf4j
public class IndexController {

    @Resource
    private GoodsService service;

    @InitBinder
    protected void initBinder(HttpServletRequest request,
                              ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping(value = "/")
    public String indexPage(){
        return "forward:/index";
    }
    @RequestMapping(value = "/index")
    public ModelAndView index(){
        return new ModelAndView("index");
    }
    @RequestMapping(value = "/goods")
    public ModelAndView goods(){
        MicroShop user = getUser();
        List<Tag> tags = service.findAllTags(user);
        return new ModelAndView("goods","tags",tags);
    }
    @GetMapping("tag")
    public String tagPage(){
        return "tag";
    }
    @RequestMapping(value = "/sku")
    public ModelAndView sku(String serialNo,Long goodsId){
        ModelAndView mv = new ModelAndView("sku");
        mv.addObject("serialNo",serialNo);
        mv.addObject("goodsId",goodsId);
        return mv;
    }

    @RequestMapping(value = "goodsTable")
    @ResponseBody
    public DataTablePage<Goods> goodsTable(@RequestParam(value = "start", defaultValue = "0") Integer page,
                                           @RequestParam(value = "length", defaultValue = "10") Integer length,Long tagId){
        Pageable request = new PageRequest(page/length,length);
        Page<Goods> goodsList;
        MicroShop user = getUser();
        if(tagId == null){
            goodsList= service.findAllGoods(user,request);
        }else{
            goodsList = service.findGoodsByTagId(tagId,request);
        }
        return new DataTablePage<>(goodsList);
    }

    private MicroShop getUser() {
        return (MicroShop) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
    }

    @RequestMapping(value = "history")
    public ModelAndView historyPage(){
        return new ModelAndView("history");
    }

    @ResponseBody
    @RequestMapping("historyData")
    public DataTablePage<InOutHistory> historyData(Date startDate, Date endDate, Integer type, @RequestParam(value = "start", defaultValue = "0") Integer page,
                                          @RequestParam(value = "length", defaultValue = "10") Integer length){
        if(startDate == null){
            startDate = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
        }
        if(endDate == null){
            endDate = new Date();
        }
        Pageable request = new PageRequest(page/length,length);
        MicroShop shop = getUser();
        return new DataTablePage<>(service.findHistory(shop,startDate, endDate, type, request));
    }
}
